package com.guigu.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.base.entity.TransportLineType;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.base.mapper.TransportLineTypeMapper;
import com.guigu.base.service.TransportLineTypeService;
import com.guigu.common.utils.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 线路类型表 服务实现类
 * </p>
 *
 * @author TaoLong
 * @since 2021-11-08
 */
@Service
public class TransportLineTypeServiceImpl extends ServiceImpl<TransportLineTypeMapper, TransportLineType> implements TransportLineTypeService {

    @Override
    public TransportLineType saveTransportLineType(TransportLineType TransportLineType) {
        TransportLineType.setLastUpdateTime(new Date());
        baseMapper.insert(TransportLineType);
        return TransportLineType;
    }

    @Override
    public IPage<TransportLineType> findByPage(Integer page, Integer pageSize, String typeNumber, String name,
                                                 Integer agencyType) {
        Page<TransportLineType> iPage = new Page(page, pageSize);
        LambdaQueryWrapper<TransportLineType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(name)) {
            lambdaQueryWrapper.like(TransportLineType::getName, name);
        }
        if (StringUtils.isNotEmpty(typeNumber)) {
            lambdaQueryWrapper.like(TransportLineType::getTypeNumber, typeNumber);
        }
        if (agencyType != null) {
            lambdaQueryWrapper.and(i -> i.eq(TransportLineType::getStartAgencyType, agencyType).or()
                    .eq(TransportLineType::getEndAgencyType, agencyType));

        }
        lambdaQueryWrapper.eq(TransportLineType::getStatus, Constant.DATA_DEFAULT_STATUS);
        lambdaQueryWrapper.orderBy(true, true, TransportLineType::getId);
        return baseMapper.selectPage(iPage, lambdaQueryWrapper);
    }

    @Override
    public List<TransportLineType> findAll(List<String> ids) {
        LambdaQueryWrapper<TransportLineType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ids != null && ids.size() > 0) {
            lambdaQueryWrapper.in(TransportLineType::getId, ids);
        }
        lambdaQueryWrapper.orderBy(true, true, TransportLineType::getId);
        return baseMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public void disableById(String id) {
        TransportLineType TransportLineType = new TransportLineType();
        TransportLineType.setId(id);
        TransportLineType.setStatus(Constant.DATA_DISABLE_STATUS);
        baseMapper.updateById(TransportLineType);
    }
}
